﻿using System;
using System.Collections.Generic;

namespace PrismNavigationFramework.Navigation
{
    public class ViewToUrlMappingRegistrationService : IViewToUrlMappingRegistrationService
    {
        private readonly Dictionary<Type, string> _viewToUrlMappings = new Dictionary<Type, string>();

        public void RegisterViewUrl(Type viewType, string url)
        {
            if (this._viewToUrlMappings.ContainsKey(viewType))
            {
                this._viewToUrlMappings[viewType] = url;
            }
            else
            {
                this._viewToUrlMappings.Add(viewType, url);
            }
        }

        public void RegisterViewUrl<TViewType>(string url)
        {
            this.RegisterViewUrl(typeof(TViewType), url);
        }

        public string GetViewUrl(Type viewType)
        {
            if (this._viewToUrlMappings.ContainsKey(viewType))
            {
                return this._viewToUrlMappings[viewType];
            }

            throw new Exception(string.Format("View {0} does not have the URL mapping therefore it cannot be loaded to MainRegion properly.", viewType.Name));
        }
    }
}